﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infrastructure.CrossCutting.Arguments;
using Infrastructure.CrossCutting.Configuration;
using Infrastructure.CrossCutting.Messages;
using Infrastructure.Data.NH.Core.Repositories;
using Soleil.Domain.Core.Repositories;
using Soleil.Domain.ebrary;

namespace GreenGrassland
{
    class Program
    {
        static void Main(string[] args)
        {
            //string arg =
            //    "-C:config.xml --TempDir:C:\\Temp -T:2012.12.12_15:00:00 -O:12345 --UserName:HichamChfiri -P:2.15 -S --Go";

            //ProgramParameters pars = new ProgramParameters();

            //BoolMessageItem<Arguments> result = Arguments.Accept(args, '-', "--", ":", pars);

            //if(!result.Success)
            //    Console.WriteLine(result.Message);

            
            //Console.WriteLine(MyConfiguration.Instance.StartTime);

            Repository<Author> repo = new Repository<Author>();

            Author a = new Author
                           {
                               AuthorName = "Hicham Chfiri" 
                           };

            repo.Add(a);

            repo.UnitOfWork.Commit();

            Console.ReadLine();
        }

        public class ProgramParameters
        {
            [Argument("C", "ConfigFile", "Path to the ConfigurationFile", false, true, typeof(FileInfo), null, "-C:config.xml")]
            public FileInfo ConfigFile { get; set; }
            [Argument("D", "TempDir", "Temp Directory", false, true, typeof(DirectoryInfo), null, "-D:C:\\temp")]
            public DirectoryInfo TempDirectory { get; set; }
            [Argument("T", "StartTime", "Start Time", false, true, typeof(DateTime), null, "2012.12.12 15:00:00")]
            public DateTime StartTime { get; set; }
            [Argument("o", "CompId", "Component Id", false, true, typeof(int), null, "12345")]
            public int ComponentId { get; set; }
            [Argument("U", "UserName", "The UserName", false, true, typeof(string), null, "Hicham")]
            public string UserName { get; set; }
            [Argument("P", "Pi", "Pi Value", false, true, typeof(double), null, "3.14")]
            public double Pi { get; set; }
            [Argument(ShortName="S", LongName="SilentMode", Description="Run Application in Background", ValueType=typeof(bool))]
            //[Argument("S", "SilentMode", "Run Application in Background", false, true, typeof(bool))]
            public bool SilentMode { get; set; }
            [Argument("G", "Go", "Go Go", true, false, typeof(bool))]
            public bool Go { get; set; }

        }

        [ConfigurationFile(ConfigFilePath = "config.xml", Name = "LocalConfiguration")]
        public class MyConfiguration
        {
            private static volatile object syncLock = new object();

            private static MyConfiguration instance;
            private MyConfiguration()
            {}

            public static MyConfiguration Instance
            {
                get
                {
                    if(instance == null)
                    {
                        lock(syncLock)
                        {
                            if(instance == null)
                            {
                                instance = new MyConfiguration();
                                BoolMessage parseResult = ConfigurationParser.Parse(instance);

                                if(!parseResult.Success)
                                    throw new Exception("Failed to Parse the config file: "+parseResult.Message);
                            }
                        }
                    }
                    return instance;
                }
            }

            [ConfigurationProperty(Name = "name", SectionName = "private")]
            public string FullName { get; set; }

            [ConfigurationProperty(Name = "time")]
            public DateTime StartTime { get; set; }

            [ConfigurationProperty(Name = "versuch", DefaultValue = "5")]
            public int TryNumber { get; set; }
        }
    }
}
